package com.cskaoyan.wxcontroller;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.bean.domain.Address;
import com.cskaoyan.bean.domain.User;
import com.cskaoyan.service.AddressService;
import com.cskaoyan.utils.ParameterValidationUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;


/**
 * @User: Yr
 * @Date: 2021/12/2
 * @Description: 小程序商城-地址管理模块-控制层
 */

@RestController
@RequestMapping("wx/address")
public class AddressController {

    @Autowired
    AddressService addressService;


    /**
     * 获取当前账户下的全部地址信息
     * @Description: 仅显示deleted为false的地址
     */
    @RequestMapping("list")
    public BaseRespVo list() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        BaseData data = addressService.list(userId);
        return BaseRespVo.ok(data);
    }


    /**
     * 获取指定的地址信息
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        Address address = addressService.detail(id);
        return BaseRespVo.ok(address);
    }

    /**
     * 新增/更新地址数据
     * @Description: 参数校验 电话为11位
     */
    @RequestMapping("save")
    public BaseRespVo save(@RequestBody @Valid Address address, BindingResult bindingResult, HttpSession session) {
        ParameterValidationUtil.paramValidation(bindingResult);
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        address.setUserId(user.getId());
        int id = addressService.save(address);
        return BaseRespVo.ok(id);
    }

    /**
     * 删除地址信息 逻辑删除
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Address address) {
        addressService.delete(address);
        return BaseRespVo.ok(null);
    }


}
